package com.un.web.topic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.core.utils.TokenUtils;
import com.un.web.topic.domian.entity.*;
import com.un.web.topic.domian.vo.TopicUserTopicVO;
import com.un.web.topic.domian.vo.TopicVO;
import com.un.web.topic.mapper.TopicJavaAnswerMapper;
import com.un.web.topic.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author xingjian
 * @Date 2023-02-06
 **/
@Service
public class TopicServiceImpl extends ServiceImpl<TopicJavaAnswerMapper, TopicJavaAnswer> implements TopicService {

    @Autowired
    private TopicJavaService topicJavaService;

    @Autowired
    private TopicJavaAnswerService topicJavaAnswerService;

    @Autowired
    private TopicTypeService topicTypeService;

    @Autowired
    private TopicAuditService topicAuditService;

    @Autowired
    private TopicUserTopicService topicUserTopicService;



    @Override
    public boolean saveTopic(Integer id) {
        TopicAudit topicAudit = topicAuditService.getById(id);
        TopicType topicType = topicTypeService.getById(topicAudit.getTopicTypeId());
        switch (topicType.getName()) {
            case "java":
                TopicJava topicJava = new TopicJava();
                topicJava.setTopicTypeId(topicAudit.getTopicTypeId());
                topicJava.setComplexity(topicAudit.getTopicComplexity());
                topicJava.setTitle(topicAudit.getTopicTitle());
                topicJava.setCheckStatus(1);
                topicJavaService.saveOrUpdate(topicJava);
                TopicJavaAnswer topicJavaAnswer = new TopicJavaAnswer();
                topicJavaAnswer.setAnswerTitle(topicAudit.getTopicAnswer());
                topicJavaAnswer.setTopicId(topicJava.getId());
                topicJavaAnswerService.save(topicJavaAnswer);
                topicAuditService.removeById(id);
                break;
            case "python":
                break;
        }
        return true;
    }

    @Override
    public void refuse(Integer id) {
        TopicAudit topicAudit = new TopicAudit();
        topicAudit.setId(id);
        topicAudit.setCheckStatus(0);
        topicAuditService.saveOrUpdate(topicAudit);
    }

    @Override
    public TopicVO randomTopic() {
        Integer userId = TokenUtils.getUserId();
        return new TopicVO();

    }

    @Override
    public TopicVO getNewTopic(Integer topicTypeId) {
        Integer userId = TokenUtils.getUserId();
        TopicType topicType = topicTypeService.getById(topicTypeId);
        switch (topicType.getName()){
            case "java":
                TopicUserTopic topicUserTopic=topicUserTopicService.getLastTopic(userId);
                if (StrUtil.isEmptyIfStr(topicUserTopic)){
                    return topicJavaService.selectOnefirst();
                }
                TopicVO topicVO = topicJavaService.selectOrderId(topicUserTopic.getTopicId());
                if (StrUtil.isEmptyIfStr(topicVO)){
                    return topicJavaService.selectOnefirst();
                }
                System.out.println(topicVO.toString());
                return topicVO;
            case "python":
                break;
        }
        return null;
    }

    @Override
    public TopicVO getNextTopic(Integer topicId, Integer typeId) {
        Integer userId = TokenUtils.getUserId();
        TopicType topicType = topicTypeService.getById(typeId);
        switch (topicType.getName()){
            case "java":
                return topicJavaService.getNextTopic(topicId);
            case "python":
                break;
        }
        return null;
    }

}
